package org.rsbot.script.wrappers;

import org.rsbot.bot.Bot;
import org.rsbot.client.RSGroundEntity;
import org.rsbot.client.RSGroundObject;
import org.rsbot.script.Methods;

/**
 * Represents an item on a tile.
 *
 * @author Jacmob
 */
public class RSGroundItem {

    private final Methods methods;
    private final RSItem groundItem;
    private final RSTile location;

    public RSGroundItem(final RSTile location, final RSItem groundItem) {
        this.location = location;
        this.groundItem = groundItem;
        this.methods = Bot.methods;
    }

    /**
     * Gets the top model on the tile of this ground item.
     *
     * @return The top model on the tile of this ground item.
     */
    public RSModel getModel() {
        final int x = location.getX() - Bot.getClient().getBaseX();
        final int y = location.getY() - Bot.getClient().getBaseY();
        final int plane = Bot.getClient().getPlane();
        final org.rsbot.client.RSGround rsGround = Bot.getClient().
                getRSGroundArray()[plane][x][y];

        if (rsGround != null) {
            final RSGroundEntity obj = rsGround.getGroundObject();
            if (obj != null) {
                final org.rsbot.client.Model model =
                        ((RSGroundObject) rsGround.getGroundObject()).getModel();
                if (model != null) {
                    return new RSAnimatedModel(Bot.methods, model, obj);
                }
            }
        }
        return null;
    }

    /**
     * Performs the given action on this RSGroundItem.
     *
     * @param action The menu action to click.
     * @return <tt>true</tt> if the action was clicked; otherwise <tt>false</tt>.
     * @see org.rsbot.script.wrappers.RSGroundItem#interact(String)
     */
    public boolean action(final String action) {
        return action(action, null);
    }

    /**
     * Performs the given action on this RSGroundItem.
     *
     * @param action The menu action to click.
     * @param option The option of the menu action to click.
     * @return <tt>true</tt> if the action was clicked; otherwise <tt>false</tt>.
     * @see org.rsbot.script.wrappers.RSGroundItem#interact(String, String)
     */
    public boolean action(final String action, final String option) {
        final RSModel model = getModel();
        if (model != null) {
            return model.action(action, option);
        }
        return methods.atTile(getLocation(), methods.random(0.45, 0.55),
                methods.random(0.45, 0.55), 0, action, option);
    }

    public RSItem getItem() {
        return groundItem;
    }

    public RSTile getLocation() {
        return location;
    }

    public boolean isOnScreen() {
        final RSModel model = getModel();
        if (model == null) {
            return methods.tileOnScreen(location);
        } else {
            return methods.pointOnScreen(model.getPoint());
        }
    }
}
